package com.iengine.services.api.command.music;

import com.iengine.services.api.utils.Constant;
import com.iengine.services.api.utils.OperationName;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("unchecked")
public class CommandMediaControl extends AbstractMusicPhoneApiCommand {

	public static final String FIELD_OPERATION = "Operation";
	public static final String FIELD_SPEC_OPERATION = "SpecOperation";
	public static final String FIELD_CONTENT = "Content";
	
	public static final String MEDIA_SESSION_ID="mediaSessionID";
	public static final String SONG_ID="songID";
    public static final String SONG_SOURCE_TYPE="songSourceType";
    public static final String RESULT_CONSTANT="constant";
    
    public static final String FIELD_CMDTYPE = "cmdType";
    public static final String MEDIA_CONTROL = "mediaControl";
    
	/**
	 * ClassName[String]: CommandMediaControl
	 * arg-list:
	 * 	Operation[String]:暂停/停止
	 * 	SpecOperation[String]:切换/开始（只有这两个内容）
	 * 	Content[String]:歌曲/笑话/故事
	 */
	@Override
	protected Map formatArgs(Map map, Map nlpParams) {
		// TODO Auto-generated method stub
		String operation = getValue(nlpParams.get(FIELD_OPERATION), "");
		String specOperation = getValue(nlpParams.get(FIELD_SPEC_OPERATION), "");
		String content = getValue(nlpParams.get(FIELD_CONTENT), "");
		Map<String,Object> resumeMap=new HashMap<String, Object>();
		Map<String,Object> subMap=new HashMap<String,Object>();
		if(StringUtils.isNotBlank(operation)){
			if(StringUtils.equals(operation, OperationName.mediaPause.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaPause.name());
			}
			if(StringUtils.equalsIgnoreCase(operation, OperationName.mediaResume.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaResume.name());
				subMap.put(MEDIA_SESSION_ID, getValue(map.get(MEDIA_SESSION_ID),null));
				subMap.put(SONG_ID, getValue(map.get(SONG_ID),null));
				subMap.put(SONG_SOURCE_TYPE, getValue(map.get(SONG_SOURCE_TYPE),null));
			}
			if(StringUtils.equals(operation, OperationName.mediaStop.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaStop.name());
			}
			if(StringUtils.equalsIgnoreCase(operation, OperationName.mediaPrevious.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaPrevious.name());
			}
			if(StringUtils.equalsIgnoreCase(operation, OperationName.mediaOpenBluetooth.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaOpenBluetooth.name());
			}
			if(StringUtils.equalsIgnoreCase(operation, OperationName.mediaCloseBluetooth.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaCloseBluetooth.name());
			}
			if(StringUtils.equals(operation, OperationName.Next.toString()) ||
					StringUtils.equals(operation, OperationName.mediaNext.toString()) ){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaNext.name());
			}
			if(StringUtils.equalsIgnoreCase(operation, OperationName.mediaFavoriteAdd.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaFavoriteAdd.name());
			}
			if(StringUtils.equals(operation, OperationName.singleLoop.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.singleLoop.name());
			}
			if(StringUtils.equalsIgnoreCase(operation, OperationName.sequence.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.sequence.name());
			}
			if(StringUtils.equals(operation, OperationName.random.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.random.name());
			}
			if(StringUtils.equalsIgnoreCase(operation, OperationName.listLoop.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.listLoop.name());
			}
			if(StringUtils.equals(operation, OperationName.mediaStart.toString())){
				CommandMusic commandMusic=new CommandMusic();
				resumeMap=commandMusic.formatArgs(map, nlpParams);
			}
			if(StringUtils.equals(operation, OperationName.mediaQuerySingerName.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaQuerySingerName.name());
			}
			if(StringUtils.equals(operation, OperationName.mediaQuerySongName.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaQuerySongName.name());
			}
			if(StringUtils.equals(operation, OperationName.mediaFavoriteRemove.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaFavoriteRemove.name());
			}
			if(StringUtils.equals(operation, OperationName.mediaCloseSmallZhi.toString())){
				subMap.put(Constant.OPERATION_NAME, OperationName.mediaCloseSmallZhi.name());
			}
			
		}else if(StringUtils.isNotBlank(specOperation)){
			subMap.put(RESULT_CONSTANT, specOperation);
		}else{
			
		}
		if(resumeMap != null && resumeMap.size() > 0){
			return resumeMap;
		}else{
			Map<String,Object> resultMap=new HashMap<String,Object>();
			resultMap.put(Constant.COMMAND_NAME, Constant.COMMAND_MEDIA_CONTROL);
			resultMap.put(Constant.DATA, subMap);
			map.put(Constant.DATA, subMap);
//			map.put(FIELD_CMDTYPE, MEDIA_CONTROL); //媒体控制的类型为通用类型media，不是mediaControl
			map.put(Constant.COMMAND_DATA, resultMap);
			return map;
		}
	}
}
